package org.snusmumr.android.media.dialog;

import org.snusmumr.android.media.R;
import org.snusmumr.android.media.bean.LabelModel;
import org.snusmumr.android.media.dialog.ColorPickerDialog.OnColorChangedListener;
import org.snusmumr.android.media.widget.ColoredButton;

import android.app.Dialog;
import android.content.Context;
import android.content.res.Resources;
import android.content.res.TypedArray;
import android.os.Bundle;
import android.view.View;
import android.view.ViewGroup.LayoutParams;
import android.widget.Button;
import android.widget.EditText;
import android.widget.LinearLayout;

public class LabelDialog extends Dialog {
	private static int colorIndex = 0;
	
	public static interface OnSaveItemListener {
		public void onLabelChanged (Long labelID, String label, int color);
	}

	private OnSaveItemListener changeListener;
	private ColoredButton coloredButton;
	private LabelModel model;

	public LabelDialog(Context context, LabelModel model, OnSaveItemListener changeListener) {
		super(context);
		this.changeListener = changeListener;
		setModel(model);
	}

	public LabelDialog(Context context, LabelModel model, int theme, OnSaveItemListener changeListener) {
		super(context, theme);
		this.changeListener = changeListener;
		setModel(model);
	}
	
	private OnColorChangedListener colorChangeListener = new OnColorChangedListener() {
		@Override
		public void colorChanged(int color) {
			coloredButton.setColor(color);
		}
	};
	
	private void setModel(LabelModel model) {
		if(model == null)
			this.model = new LabelModel(null, null, null);
		else
			this.model = model;
	}
	
	private int getNextColor() {
		Resources res = getContext().getResources();
		TypedArray colors = res.obtainTypedArray(R.array.std_colors);
		if(colorIndex >= colors.length()) {
			colorIndex = 0;
		}
		System.out.println("next color: index " + colorIndex);
		return colors.getColor(colorIndex++,res.getColor(R.color.green));
	}
	
	@Override
	protected void onStart() {
		super.onStart();
		if(model.getColor() == null)
			coloredButton.setColor(getNextColor());
	}

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		
		setContentView(R.layout.label_dlg);
		setTitle("label"); //TODO title
		setCancelable(false);
		
		LayoutParams params = getWindow().getAttributes(); 
	    params.width = LayoutParams.FILL_PARENT; 
	    getWindow().setAttributes((android.view.WindowManager.LayoutParams) params);
	    
	    LinearLayout layout = (LinearLayout) findViewById(R.id.ed_label_layout);
	    android.widget.LinearLayout.LayoutParams lp = new android.widget.LinearLayout.LayoutParams(75, LayoutParams.WRAP_CONTENT);
	    lp.gravity = 0x10;
	    
	    coloredButton = new ColoredButton(getContext(), model.getColor());
		layout.addView(coloredButton, lp);
		coloredButton.setOnClickListener(new View.OnClickListener() {
			public void onClick(View v) {
				ColorPickerDialog dlg = new ColorPickerDialog(
						LabelDialog.this.getContext(), 
						colorChangeListener, 
						coloredButton.getColor());
				dlg.show();
			}
		});
	    
	    Button save = (Button) findViewById(R.id.btn_label_dlg_save);
		save.setOnClickListener(new View.OnClickListener() {
			public void onClick(View v) {
				changeListener.onLabelChanged(
						model.getId(), 
						getEditText().getText().toString(), 
						coloredButton.getColor());
				LabelDialog.this.dismiss();
			}
		});
		Button canel = (Button) findViewById(R.id.btn_label_dlg_cancel);
		canel.setOnClickListener(new View.OnClickListener() {
			public void onClick(View v) {
				LabelDialog.this.cancel();
			}
		});
		
		if(model.getValue() != null)
			getEditText().setText(model.getValue());
	}
	
	private EditText getEditText() {
		return (EditText) findViewById(R.id.et_label_item);
	}

}
